package com.smart.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/*数据库连接类  该类中包含一个学生登录的信息与数据库中stu表数据对照的方法和一个获得本实例的静态方法*/
public class DBoperator2 {
    public boolean insert(String name,String password){
        final String DRIVER="com.mysql.jdbc.Driver";
        final String USER="root";
        final String PWD="147852369";
        final String URL="jdbc:mysql://localhost:3306/student";
        Connection connection;
        try {
            Class.forName(DRIVER);
            System.out.println("Connecting to a selected database...");
            connection = DriverManager.getConnection(URL,USER,PWD);
            System.out.println("Connected database successfully...");

            System.out.println("Creating statement...");
            Statement statement= connection.createStatement();
            String sql="SELECT id,password FROM stu WHERE id='" + name + "' and password='" + password + "';";
            //statement.execute(sql);
            System.out.println("执行成功");
            ResultSet sc = statement.executeQuery(sql);
            //System.out.println(sc.next());
            if (sc.next()) {//移动记录指针
                System.out.println("登陆成功!");
                connection.close();
                return true;
            } else {
                System.out.println("账号密码错误");
                connection.close();
                return false;
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}